package com.acceptable.qucun.user.controller;


import com.acceptable.qucun.generic.util.result.success.UserSuccessResult;
import com.acceptable.qucun.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 巴斯罗斯·K·赫斯洛利
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/user")
//@CrossOrigin(value = "*") // 允许跨源访问
public class UserController{
    @Autowired private UserService userService;

    @PostMapping("/verify_code")
    public String sendVerifyCode(@RequestAttribute("data_map") Map<String, String> dataMap,
                                 HttpServletRequest request){
        userService.sendVerifyCode(dataMap, request);
        return UserSuccessResult.VERIFY_CODE_GET_SUCCESS.getCode();
    }

    @PostMapping("/register")
    public String register(@RequestAttribute("data_map") Map<String, String> dataMap){
        userService.registerUnboundByVerifyCode(dataMap);
        return UserSuccessResult.REGISTER_SUCCESS.getCode();
    }

    @PostMapping("/reset_pwd")
    public String resetPassword(@RequestAttribute("data_map") Map<String, String> dataMap){
        return userService.resetPwdBoundByVerifyCode(dataMap);
    }

    @GetMapping("/init_info/{user_id}/{current_dict_id}")
    public Map<String, Object> getInitInfo(@PathVariable(value = "user_id") Integer userId,
                                           @PathVariable(value = "current_dict_id") Long currentDictId){
        return userService.getInitInfo(userId, currentDictId);
    }

    @GetMapping("/init_user_info/{user_id}")
    public Map<String, Object> getInitUserInfo(@PathVariable("user_id") Integer userId){
        return userService.getInitUserInfo(userId);
    }

    @PostMapping("/update_info")
    public Map<String, Object> updateNormalInfo(@RequestAttribute("data_map") Map<String, String> dataMap){
        return userService.updateNormalInfo(dataMap);
    }

    @PostMapping("/head_img_upload")
    public String headImgUpload(@RequestAttribute("data_map") Map<String, String> dataMap,
                                @RequestPart("raw_img_data") byte[] imgData){
        return userService.headImgUpload(dataMap, imgData);
    }

    @GetMapping("/head_img_show")
    public void headImgShow(HttpServletResponse response,
                            @RequestParam("user_id") Integer userId){
        userService.headImgShow(response, userId);
    }

    @GetMapping("/head_img_download")
    public ResponseEntity<String> headImgDownload(@RequestParam("user_id") Integer userId){
        return userService.headImgDownload(userId);
    }

    @PostMapping("/change_email_verify")
    public String changeEmailVerify(@RequestAttribute("data_map") Map<String, String> dataMap){
        return userService.changeEmailVerifyByVerifyCode(dataMap);
    }

    @PostMapping("/change_email")
    public String changeEmail(@RequestAttribute("data_map") Map<String, String> dataMap){
        return userService.changeEmailByRedis(dataMap);
    }

    @PostMapping("/drop_account")
    public String dropAccount(@RequestAttribute("data_map") Map<String, String> dataMap){
        return userService.dropBoundAccountByRedis(dataMap);
    }

    // 登录处理由登录验证负责 此处提供一个虚地址
    @RequestMapping("/login")
    public String loginHandler(){
        return "";
    }

    // 同登录
    @GetMapping("/logout")
    public String logout(){
        System.out.println("com.acceptable.web.qucun.controller.UserController.logout");
        return "";
    }

    // 测试用
    @GetMapping("/success")
    @ResponseBody
    public String success(){
        System.out.println("com.acceptable.web.qucun.controller.UserController.success");
        return "login_success";
    }

}

